//管理tools相关状态
import { defineStore } from 'pinia'
import { getToolsCate, getToolList } from '@/components/tools/tools'

export const useToolsStore = defineStore('tools', {
  //用来存放变量
  state: () => ({
	//工具列表
    toolList: null,
    toolInfo: null ,
	//工具分类
    toolsCate: null
  }),
  //方法
  actions: {
	//初始化
	async init() {
		this.toolList = await getToolList()
		this.toolsCate = await getToolsCate()
	},
    //筛选获取工具
    async getToolInfos(data) {
      const { cateId, title, route } = data
      //获取工具list
      let list = this.toolList
      //标题筛选
      if (title != '') {
        list = list.filter(item => {
          let tmpValue = item.title.toLowerCase()
          let tmpDesc = item.desc.toLowerCase()
		  let tmpUrl = item.url.toLowerCase()
          return tmpValue.indexOf(title.toLowerCase()) !== -1 || tmpDesc.indexOf(title.toLowerCase()) !== -1 || tmpUrl.indexOf(title.toLowerCase()) !== -1;
        });
      }
      //分类筛选
      if (cateId > 0) {
        list = list.filter(item => {
          return item.cateId == cateId;  
        });
      }
      //路由筛选
      if (route && route != '') {
        list = list.filter(item => {
          return item.url.indexOf(route) !== -1;
        });
      }
      return list
    }
  }
})